define(function(require, exports, module) {
  var moment = require('moment'),
      _ = require('underscore'),
      Backbone = require('backbone');

  module.exports = Backbone.View.extend({
    tagName: 'li',
    className: 'swipeout',
    template: require('tpl/plan/item.handlebars'),
    events: {
      'click .party-edit': 'onEdit',
      'click .party-remove': 'onRemove'
    },
    initialize: function(options) {
      //模型上的监听事件
      this.listenTo( this.model, 'change', this.render);
      this.listenTo( this.model, 'destroy', this.remove);

      //传入gomac
      this.gomac = options.gomac;

      console.log(this.model);
    },
    render: function() {
      var label = '',
          isEnded = this.model.time.isEnded(),
          isStarted = this.model.time.isStarted(),
          _startAt = this.model.time.get('startAt'),
          _endAt = this.model.time.get('endAt');
      if(isEnded) { //已经结束
        label = '已结束';
      }
      else if(isStarted) { //进行中
        label = moment.unix(_endAt).fromNow() + '结束';
      }
      else { //即将开始
        label = moment.unix(_startAt).fromNow() + '开始';
      }
      var data = this.model.toJSON();
      //数据格式转换
      data.startAt = moment.unix(_startAt).format('YYYY-MM-DD HH:mm');
      data.endAt = moment.unix(_endAt).format('YYYY-MM-DD HH:mm');
      //添加到视图中
      $(this.el).html( this.template({
        model: data,
        label: label,
        canInvite: data.owner && !isStarted,
        canEdit: data.owner && !isEnded
      }) );
      //返回当前视图
      return this;
    },
    onEdit: function(e) {
      var self = this,
          content = require('tpl/plan/form.handlebars');
      
      var _popup = self.gomac.popup( '<div class="popup">'+content()+'</div>', true);
      var popup = $(_popup);

      var form = popup.find('.meeting-form');
      
      //初始化表单
      this.gomac.formFromJSON(form, this.model.toForm());

      //日期控件
      var datepicker = require('datepicker');
      datepicker({});
      
      popup.find('.button-submit').on('click', function(e) {
        
        //获取表单数据
        var data = self.gomac.formToJSON(form);

        //更新到模型中
        var rtn = self.model.fromForm(data);

        if(rtn.code === 0) {
          self.model.save(null, {
            success: function(model, response, options) {
              self.gomac.closeModal(popup);
              self.render();
            },
            error: function(model, response, options) {
              self.gomac.alert('保存安排时出错');
              console.log('保存安排时出错', model, response, options);
            }
          });
        } else {
          self.gomac.alert(rtn.message);
          console.log('表单数据有误', rtn);
        }
      });
    },
    onRemove: function(e) {
      var self = this;
      self.gomac.confirm('确定删除安排《'+self.model.get('topic')+'》吗？', function(){
        self.model.time.destroy({
          success: function(time, resp, options) {
            self.model.destroy({
              success: function(meeting, resp, options) {
                //重置滑动操作
                self.gomac.swipeoutOpenedEl = undefined;
              },
              error: function(meeting, resp, options) {
                //错误发生了
                self.gomac.alert('无法删除《'+meeting.get('topic')+'》');
                console.log('删除安排出错', meeting, resp, options);
              }
            });
          },
          error: function(time, resp, options) {
            self.gomac.alert('无法删除时间');
            console.log('删除时间出错', time, resp, options);
          }
        });
      });
    }
  });
});